package club.baimahu.pencil.domain.entity.discount;

import club.baimahu.pencil.domain.entity.order.CommodityList;

import java.math.BigDecimal;

/**
 * 按比例打折
 */
public  class PercentDiscountStrategy implements DiscountStrategy{

    private double percent;

    private Condition condition;

    public PercentDiscountStrategy(double percent){
        if(percent <=0 || percent >=1){
            throw new IllegalArgumentException("percent is illegal");
        }
        this.percent = percent;
    }

    public PercentDiscountStrategy(double percent,Condition condition){
        this.condition = condition;
        if(percent <=0 || percent >=1){
            throw new IllegalArgumentException("percent is illegal");
        }
        this.percent = percent;
    }

    @Override
    public BigDecimal discount(CommodityList commodityList) {
        BigDecimal originalTotal = commodityList.totalMoney();
        if(condition == null || condition.fit(commodityList)){
            return originalTotal.multiply(BigDecimal.valueOf(percent));
        }else{
            return originalTotal;
        }
    }

    @Override
    public void setCondition(Condition condition) {
        this.condition = condition;
    }


}
